package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarRankMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_UNION_WAR_GET_REWARD, desc = "领取群英争霸排行榜奖励")
public class CrossUnionWarGetRewardCmd extends CrossUnionCommand {
    private int activityId;
    private int conditionId;
    private long userId;
    private String unionUid;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        unionUid = reqMsg.getUnionUid();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int myRank = -1;
        ActivityConditionInfo activityConditionInfo = CrossUnionWarActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if (activityConditionInfo != null) {
            myRank = CrossUnionWarRankMgr.getUnionWarRank(activityId, activityConditionInfo.getType(), serverId, unionUid, packet.getPlayerId());
        }
        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(conditionId);
        respMsg.setRank(myRank);
        respMsg.setUnionId(unionUid);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_UNION_WAR_GET_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);

        //有人领奖  mark发奖
        /*ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(activityId);
        if(activityInfo != null){
            //可领取的最低排名
            int lowestRank = CrossUnionHonorsActivityMgr.getLowestRewardRank(activityInfo.getServerIdList().size(), eHonorsActivityType.UNION_HONORS_UNION_PHASE_WAR);
            List<String> topUnions = CrossUnionWarMgr.getTopUnions(activityInfo.getActivityId(), lowestRank, 0);
            if (topUnions != null) {
                if (!topUnions.isEmpty()) {
                    //CrossUserMgr.getLogger().debug("----  reward serverId {}", serverId);
                    CrossUnionHonorsActivityMgr.add(activityInfo.getActivityId(), topUnions, activityInfo.getType(), activityInfo.getChildType(), activityInfo.getServerIdList(),serverId);
                }
            }
        }*/
    }

    @Override
    public void sendData() {

    }
}
